package shop

import (
	"day12_16_shop/internal/api/request"
	"day12_16_shop/internal/models/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func ShopBuy(c *gin.Context) {
	var json *request.ShopBuyRequest
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	s := value.(string)
	id, _ := strconv.Atoi(s)

	buy, s2 := service.Buy(json.ShopId, id)
	if s2 != "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"message": s2,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"message": "购买成功",
		"data":    buy,
	})

}
